import Vue from 'vue'

//注册路由插件
import Router from 'vue-router'
Vue.use(Router)

//引入状态管理器
import store from '../store'

//引入组件
import login from '../views/login/login.vue'
import reg from '../views/login/reg.vue'
import home from '../views/home/index.vue'
import technician from '../views/technician/index.vue'
import my from '../views/my/index.vue'
import changeInformation from '../views/my/changeInformation.vue'
import information from '../views/my/information.vue'
import orders from '../views/my/orders.vue'
import common from '../views/common/index.vue'
import projectDetails from '../views/projectDetails/index.vue'
import proReservation from '../views/proReservation/index.vue'
import myOrder from '../views/myOrder/index.vue'
import technicianDetail from '../views/technicianDetail/index.vue'
import goodsClass from '../views/goodsClass/index.vue'
import goodsCart from '../views/goodsCart/index.vue'
import goodsDetail from '../views/goodsDetail/index.vue'
import address from '../views/goodsCart/address.vue'
import onAddAddress from '../views/goodsCart/onAddAddress.vue'
import onEditAddress from '../views/goodsCart/onEditAddress.vue'
import search from '../views/search/index.vue'

//路由的实例化和配置
const router=new Router({
    mode:'history',
    routes:[
        {path:'/',redirect:'/login'},
        {path:'/login',component:login,meta:{canLogin:true}},
        {path:'/reg',component:reg,meta:{canLogin:true}},
        {path:'/home',component:home},
        {path:'/technician',component:technician},
        {path:'/technicianDetail',component:technicianDetail},
        {path:'/my',component:my},
        {path:'/information',component:information},
        {path:'/changeInformation',component:changeInformation},
        {path:'/orders',component:orders},
        {path:'/projectDetails',component:projectDetails},
        {path:'/proReservation',component:proReservation},
        {path:'/myOrder',component:myOrder},
        {path:'/goodsClass',component:goodsClass},
        {path:'/goodsCart',component:goodsCart},
        {path:'/goodsDetail',component:goodsDetail},
        {path:'/address',component:address},
        {path:'/onAddAddress',component:onAddAddress},
        {path:'/onEditAddress',component:onEditAddress},
        {path:'/search',component:search},
        {path:'*',component:common,meta:{canLogin:true}},
    ]
})
//路由全局拦截
router.beforeEach(function(to,from,next){
    if(to.meta.canLogin){
        next()
    }else{
        let token=store.getters['common/getToken']
        if(!!token){
            next()
        }else{
            next('/login')
        }
    }
})


export default router